// pass: 100%
function solution(n) {
    let helper = function (n) {
        if (n < 3) return memo[n];
        if (memo[n] !== -1) return memo[n];
        return memo[n] = helper(n - 1) + helper(n - 2) + helper(n - 3);
    }

    let memo = new Array(n + 1).fill(-1);
    memo[0] = 0;
    memo[1] = 1;
    memo[2] = 2;

    let res = helper(n);

    return res;
}

console.log(solution(4));